<html>
<head>
	<meta charset='utf-8' />
	<title>小区信息助手后台</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		@media screen and (max-width:450px){
			#content,.nav_div{width:98%;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#content,.nav_div{width:450px;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
	</style>
</head>
<body>
<div id='content' style='position:relative;border:0px solid'>
	<h1 style='text-align:center'>服务器管理</h1>

	<div>
		<input type="text" id='add_server_input' style='margin-left:13px;width:250px' placeholder='添加新服务器,如:http://www.a.top' />
		<button id='add_server_submit'>添加</button>
		<button id='add_server_reset'>重置</button>
	</div>
	

	<!--地址列表-->
	<div id='addr_list'></div>


	<div style='text-indent:13px' id='del_area'>
		<button id='trans'>反选</button>
		<button onclick='DelDomains()'>删除选中</button>
		<button id='reset_cache'>刷新缓存</button>
	</div>

	<div style='font-size:12px;color:red;margin:20px 0;text-align:center'>
		注：当upyun该数据丢失时，点击'刷新缓存',即可更新数据到upyun
	</div>

</div>

</body>
</html>
<script src='../public/jquery.js'></script>
<script src='../public/fun.js'></script>
<script>
$(function(){
	//刷新缓存
	$('#reset_cache').click(function(){
		sessionStorage.admin_open_domain_list = '';
		location.reload();
	});

	//反选
	$('#trans').click(function(){
		$('input[type=checkbox]').each(function(i,o){
			o.checked=!o.checked;
		});
	});


	//底部导航与显示页面
	$.ajax({
		url:'after_template_bottom_nav.html',
		success(res){
			$('body').append(res);
		}
	});

	//添加域名
	$('#add_server_submit').click(function(){
		var domain = $('#add_server_input').val().trim();
		if(!domain){
			return;
		}
		if(sessionStorage.admin_open_domain_list){
			var tmp_p = '';
			var tmp_list = JSON.parse(sessionStorage.admin_open_domain_list);
			var tmp_stat = false;
			for(var i=0;i<tmp_list.length;i++){
				if(domain == tmp_list[i][1]){
					tmp_stat = true;
				}
			}

			if(tmp_stat){
				A('域名已存在1');
				return;
			}
		}
		AjaxAfter({type:'add_domian',domain:domain},function(res){
			C(res);
			var res2 = JSON.parse(res);
			sessionStorage.admin_token1 = res2.token1;
			if(res2.stat == 'ok'){
				A('添加成功！')
				sessionStorage.admin_open_domain_list = '';
				GetDomainList();
			}else{
				A('添加失败2！')
			}

		});
	});


	
	GetDomainList();	//获取域名列表


});



//获取域名列表
function GetDomainList(){
	var v = 0;
	if(sessionStorage.admin_open_domain_list){
		if(v) C('domain_list from sess');
		ViewDomainList();
	}else{
		AjaxAfter({type:'get_domain_list'},function(res){
			if(v) C('domain_list from after');
			if(v) C(res);
			var tmp  = JSON.parse(res);
			sessionStorage.admin_open_domain_list = JSON.stringify(tmp.data);
			sessionStorage.admin_token1 = tmp.token1;
			ViewDomainList();
		});
	}
}



//显示域名列表
function ViewDomainList(list){
	if(!sessionStorage.admin_open_domain_list){
		$('#addr_list').html('');
		$('#del_area').hide();
		return;
	}
	$('#del_area').show();
	var list = JSON.parse(sessionStorage.admin_open_domain_list);
	var list_str = '';
	for(var i=0;i<list.length;i++){
		list_str += "<div style='display:flex;margin:10px'>";
			list_str += "<label style='flex:1'>";
				list_str += "<input type='checkbox' value='"+list[i][0]+"' />";
				list_str += "<span style='margin-left:30px;flex:1;text-align:center'>" +list[i][1]+ "</span>";
			list_str += "</label>";
			list_str += "<button onclick=DelDomain('"+list[i][0]+"')>删除</button>";
		list_str += "</div>";
	}
	$('#addr_list').html(list_str);
}



//单选删除
function DelDomain(id){
	if(!id){
		return;
	}
	if(!confirm('确认操作？')){
		return;
	}
	Dels(id);
}

//多选删除
function DelDomains(){
	if(!confirm('确认操作？')){
		return;
	}
	var list = '';
	$('input[type=checkbox]').each(function(i,o){
		if(o.checked){
			list += o.value+',';
		}
	});
	list = list.substring(0,list.length-1);
	Dels(list);
}


//执行删除
function Dels(domain){
	AjaxAfter({type:'del_domain',domain:domain},function(res){
		var tmp = JSON.parse(res);
		if(tmp.stat && tmp.stat == 'ok'){
			A('删除成功！');
			sessionStorage.admin_open_domain_list = '';
			sessionStorage.admin_token1 = tmp.token1;
			GetDomainList();
		}else{
			A('操作失败！');
		}
	});
}

</script>